<?php


namespace app\index\validate;


use think\Validate;

class Goods extends Validate
{
    protected $rule = [
        ['id','require|number','商品id不能为空|商品id必须为数字'],
        ['cn_name','require','中文商品名不能为空'],
        ['en_name','require','英文商品名不能为空'],
        ['type','require|number','类型不能为空'],
        ['pur_price','require|number','单价不能为空|单价必须为数字'],
        ['amount','require|number','数量不能为空|数量必须为数字'],
        ['weight','requireIf:type,1|number','重量不能为空|重量必须为数字'],
        ['image','require','图片不能为空'],
        ['warehouse','require','仓库不能为空'],
        ['selling_price','require|number','定价不能为空|定价必须为数字'],
        ['product','require|array|checkProduct:(input:\'product\')','商品信息必填|商品信息必须为数组|商品信息不能为空'],
    ];

    protected $scene = [
        'create'  =>  ['cn_name','en_name','type','pur_price','amount','weight','warehouse','selling_price'],
        'stock_in'  =>  ['id','type','pur_price','amount','weight'],
        'change_price'  =>  ['id','selling_price'],
        'update'=>['cn_name','en_name'],
        'create_batch_product'=>['product'],
        'product'=>['cn_name','en_name','type','pur_price','amount','selling_price']
    ];

    protected function checkProduct($value){
        foreach ($value as $key=>$item) {
            if(!is_array($item)){
                return false;
            }
            $data=[];
            foreach ($item as $i=>$val) {
                $data[$i] = $val;
            }
            if (false === $this->scene('product')->check($data)) {
                return $this->getError();
            }
        }
        return true;
    }
}